import sys
from PyQt5.QtWidgets import QApplication, QWidget
from calculator1 import Ui_Form


class calculator(QWidget, Ui_Form):
    def __init__(self):
        super(calculator, self).__init__()
        self.setupUi(self)
        self.button_init()

    def button_init(self):
        self.button_0.clicked.connect(self.Append_0)
        self.button_1.clicked.connect(self.Append_1)
        self.button_2.clicked.connect(self.Append_2)
        self.button_3.clicked.connect(self.Append_3)
        self.button_4.clicked.connect(self.Append_4)
        self.button_5.clicked.connect(self.Append_5)
        self.button_6.clicked.connect(self.Append_6)
        self.button_7.clicked.connect(self.Append_7)
        self.button_8.clicked.connect(self.Append_8)
        self.button_9.clicked.connect(self.Append_9)
        self.button_plus.clicked.connect(self.Append_plus)
        self.button_sub.clicked.connect(self.Append_sub)
        self.button_multi.clicked.connect(self.Append_multi)
        self.button_div.clicked.connect(self.Append_div)
        self.button_right_bracket.clicked.connect(self.Append_right_bracket)
        self.button_left_bracket.clicked.connect(self.Append_left_bracket)
        self.button_AC.clicked.connect(self.AC)
        self.button_clr.clicked.connect(self.CLR)
        self.button_equal.clicked.connect(self.calculate)

    def Append_0(self):
        self.expression = self.lineEdit.text()+'0'
        self.lineEdit.setText(self.expression)

    def Append_1(self):
        self.expression = self.lineEdit.text()+'1'
        self.lineEdit.setText(self.expression)

    def Append_2(self):
        self.expression = self.lineEdit.text()+'2'
        self.lineEdit.setText(self.expression)

    def Append_3(self):
        self.expression = self.lineEdit.text()+'3'
        self.lineEdit.setText(self.expression)

    def Append_4(self):
        self.expression = self.lineEdit.text()+'4'
        self.lineEdit.setText(self.expression)

    def Append_5(self):
        self.expression = self.lineEdit.text()+'5'
        self.lineEdit.setText(self.expression)

    def Append_6(self):
        self.expression = self.lineEdit.text()+'6'
        self.lineEdit.setText(self.expression)

    def Append_7(self):
        self.expression = self.lineEdit.text()+'7'
        self.lineEdit.setText(self.expression)

    def Append_8(self):
        self.expression = self.lineEdit.text()+'8'
        self.lineEdit.setText(self.expression)

    def Append_9(self):
        self.expression = self.lineEdit.text()+'9'
        self.lineEdit.setText(self.expression)

    def Append_right_bracket(self):
        self.expression = self.lineEdit.text()+')'
        self.lineEdit.setText(self.expression)

    def Append_left_bracket(self):
        self.expression = self.lineEdit.text()+'('
        self.lineEdit.setText(self.expression)

    def Append_plus(self):
        self.expression = self.lineEdit.text()+'+'
        self.lineEdit.setText(self.expression)

    def Append_sub(self):
        self.expression = self.lineEdit.text()+'-'
        self.lineEdit.setText(self.expression)

    def Append_multi(self):
        self.expression = self.lineEdit.text()+'*'
        self.lineEdit.setText(self.expression)

    def Append_div(self):
        self.expression = self.lineEdit.text()+'/'
        self.lineEdit.setText(self.expression)

    def AC(self):
        expression = self.lineEdit.text()
        expression = expression[: len(expression)-1]
        self.lineEdit.setText(expression)

    def CLR(self):
        self.lineEdit.clear()
        self.textBrowser.clear()

    def calculate(self):
        state = "operator"
        priority = {"(": 1, "+": 2, "-": 2, "*": 3, "/": 3, ")": 4}
        length = len(self.lineEdit.text())
        expression = self.lineEdit.text()
        i = 0
        self.operator_stack = []
        self.number_stack = []
        while i < length:
            if (expression[i] >= '0' and expression[i] <= '9'):
                begin = i
                while i + 1 < length and expression[i + 1] >= '0' and expression[i + 1] <= '9':
                    i = i + 1
                end = i + 1
                number = int(expression[begin: end])
                self.number_stack.append(number)
                state = "number"
            elif (expression[i] == '('):
                self.operator_stack.append('(')
                state = "operator"
            elif (expression[i] == ')'):
                self.OperatorPop()
                self.operator_stack.pop()
                state = "operator"
            else:
                operator = expression[i]
                if operator == '-' and state == "operator":
                    self.number_stack.append(0)
                if len(self.operator_stack) > 0:
                    TopOperator = self.operator_stack[-1]
                    if priority[operator] <= priority[TopOperator]:
                        self.OperatorPop()
                self.operator_stack.append(operator)
                state = "operator"
            i = i + 1
        self.OperatorPop()
        number = self.number_stack[-1]
        data = expression+'='+str(number)
        self.textBrowser.append(data)


    def OperatorPop(self):
        while len(self.operator_stack) != 0 and self.operator_stack[-1] != '(':
            operator = self.operator_stack.pop()
            number1 = self.number_stack.pop()
            number2 = self.number_stack.pop()
            number = 0
            if operator == '+':
                number = number2 + number1
            elif operator == '-':
                number = number2 -number1
            elif operator == '*':
                number = number2 * number1
            else:
                number =number2 / number1
            self.number_stack.append(number)


if __name__=='__main__':
    app = QApplication(sys.argv)
    demo = calculator()
    demo.show()
    sys.exit(app.exec_())